 
import { ABaseDecision } from "GameLoop/DecisionMaking/BaseDecision";
import { OutMinerialEntity } from "GameLoop/WorldState/EntityManager/Entitys";
import { priority } from "utils/constants/definetypes";
import { TaskHelper } from "utils/task/TaskHelper";
 

// 还要添加一个定时器 事件触发器
/**
 * 采集任务。元素矿 过道商品
 */
export class HarvestSingleDecision<  T extends "creep"="creep"> extends ABaseDecision<T>
{
    // 找不到任务。就新增
    public isExpired(event: HarvestEvent, region: string): boolean 
    {
        const room = Game.rooms[event.region] as Required<Room>;
        const entity = room.getEntityByID<OutMinerialEntity>(event.source);
        if(!entity)
        {
            return false;
        }
        if(entity.getNodeMemory().state)
        {
            room.deleteTaskById(event.source,"creep")
            return false
        }
        
        
        if(!entity.getMinerial()?.mineralAmount)
        {
            return false;
        }
       
        const curTask = room.findTaskById<ITask<ICreepTaskData<"harvestM">>>(event.source,"creep");
        if(curTask)
        {
            return false;
        }
        
        return true
    }
    public makeTask(event: HarvestEvent, region: string): IDecisionCode<T>|false
    {
        
        const room = Game.rooms[event.region] as Required<Room>;
    
        const entity = room.getEntityByID<OutMinerialEntity>(event.source);
        // 俩矿工俩采集工
        const tasks=[];
        const porternum = Math.ceil(entity.getPath().length/50);
        const task:ITaskDeclare["creep"]={
            id: event.source,
            priority:priority.C,
            ttl:-1,
            type:"creep",
            data:{
                type:"harvestM",
                arg:{
                    entityId:event.source,
                },
                room:event.room,
                creeps:[],
                body:{},        // 当前统计的body数量
                income: 0, // 1500T内的收入
                cost:TaskHelper.getTaskPathCost(event.region,event.room),
                required:[
                    [ ["minex"],1,1],
                    [["porter"],porternum,porternum]
                ]
            },
            shard:1,
        }
        tasks.push(task);
        if(tasks.length>0)
        {
            const code:IDecisionCode<T>={
                region:event.region,
                task:tasks,
                isunique:false,
            }
            return code
        }
        return false
    }
    
}